/** get primary cookie. **/
var primaryCookie = 'html5';
var Test_BW = 0;
var iconload='<div class="icon_loading"><img src="public/images/img/ajax-loader.gif" /></div>';

// search ajax -- tim kiem
$(function(){
	var cssObj = { 'box-shadow' : '#888 1px 2px 2px', // Added when CSS3 is standard
		'-webkit-box-shadow' : '#888 1px 2px 2px', // Safari
		'-moz-box-shadow' : '#888 1px 2px 2px'}; // Firefox 3.5+
	$("div.suggestions").css(cssObj);
	 $(".search_title").blur(function(){
		$('div.suggestions').fadeOut();
	 });
});
function lookup(e,inputString) {
	var kt=false;	
	var typesearch = "all";
	if(inputString.length <2) {
		$('div.suggestions').fadeOut(); // Hide the suggestions box
	} else {
		if(e!=1){
			var keypressed = null;	
			if (window.event){
				keypressed = window.event.keyCode; //IE
			}else{ 
				keypressed = e.which; //NON-IE, Standard
			}		
			if (keypressed == 13){ 
				kt=true;
			}
		}else{
			kt=true;
		}
		if(kt==true){	
			document.getElementById("searchform").submit=true;			
		}else{
			$.post("/searchfilm.html", {queryString: ""+inputString+"", queryString1: ""+typesearch+""}, function(data) { // Do an AJAX call				
				$('div.suggestions').fadeIn(); // Show the suggestions box
				$('div.suggestions').html(data); // Fill the suggestions box
			});	
		}
	}
}
function SearchFilm(e){		
	var course=document.getElementById("inputString").value;
	var typesearch=document.getElementById("typesearch").value;
	window.location="/search/"+typesearch+"/"+course+".html";
}
// search ajax -- tim kiem

function HideFb() {
   $("#play_fb").hide();
}
function setTimecookie(id){
	setCookie("cookie_load_id",id,1);
	jwplayer().onTime(function (event) {		
		var timeload=event.position;	
		timeload=Math.round(timeload-1);
		if (timeload>1 && timeload%5==0) {				
			setCookie("cookie_load_time",timeload,1);					
		}
	});  
}
function DelTimecookie(id){
	setCookie("cookie_load_id","",-1);	
	setCookie("cookie_load_time","",-1);	
	//document.cookie = 'cookie_load_id=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
	//document.cookie = 'cookie_load_time=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';	
}
function setTimecookie1(id){
	jwplayer().onTime(function (event) {			
		$("#play_tb").hide();		
		var timeload=event.position;	
		timeload=Math.round(timeload-1);
		if(timeload>8){
			$("#play_fb").hide();
		}		
		if(timeload>3 && timeload%2==0){
			setCookie("cookie_load_play",id,1,'/');	
		}
		if(timeload==1){
			KT_Timecookie(0,id);	
		}else if(timeload>1 && timeload%5==0){
			KT_Timecookie(1,id);	
		}
		
	});  
}
function getTimecookie(id){	
	var cookie_load_time = getCookie("cookie_load_time");
	var cookie_load_id = getCookie("cookie_load_id");
	if(cookie_load_time){
		if(cookie_load_id==id){			
			jwplayer().seek(cookie_load_time);
		}
	} 	
}
function KT_Timecookie(time,id){	
	if(time==0){
		setCookie("cookie_load_play",id,1,'/');	
	}	
	setTimeout(function(){
		var cookie_load_play = getCookie("cookie_load_play");	
		if(!isNaN(cookie_load_play) && cookie_load_play!=id){
			$("#play_tb").show();
			jwplayer().stop();
		}
	},2200);
}
	
function setCookie(c_name,value,exdays,part)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	if(exdays!=null && part=='/'){
		c_value=c_value+"; path=/";
	}
	document.cookie=c_name + "=" + c_value;
}
function getCookie(c_name)
{
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++)
	{
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name)
		{
			return unescape(y);
		}
	}
}
function GetNext(id){	
	var link_load = '/randpart/'+id+'.html';
	var xmlhttp;	
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{				
			document.getElementById('bandwitch').value=xmlhttp.responseText;		
		}
	  }					
		xmlhttp.open("GET",link_load,true);	
	xmlhttp.send();	
}
function LoadNext(now, id){	
	GetNext(id);
	var str = trim(document.getElementById('bandwitch').value);
	jwplayer().load(str);	
	jwplayer().play();
}
function createXMLHttp(){
	if(typeof XMLHttpRequest != "undefined"){
		return new XMLHttpRequest();
	}else if(window.ActiveXObject){
		var aVersions = ["MSXML2.XMLHttp.5.0",
	"MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0",
	"MSXML2.XMLHttp","Microsoft.XMLHttp"];
		
		for(var i = 0; i < aVersions.length; i++){
			try{
				var oXMLHttp = new ActiveXObject(aVersions[i]);
				return oXMLHttp;
			}catch(oError){
				//Do nothing
			}
		}
	}
	
	throw new Error("XMLHttp object could be created.");
}
function loadAjaxGet(link_load,div_load){
	document.getElementById(div_load).innerHTML=iconload;		
	var xmlhttp;	
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{				
			document.getElementById(div_load).innerHTML=xmlhttp.responseText;		
		}
	  }					
		xmlhttp.open("GET",link_load,true);	
	xmlhttp.send();	 	
}
function loadAjaxPost(link_load,params,div_load){
	document.getElementById(div_load).innerHTML=iconload;			
	var xmlhttp;
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	if (xmlhttp.readyState == 4 || xmlhttp.readyState == 0) {	
		document.getElementById(div_load).innerHTML = iconload;	
		xmlhttp.open("POST", link_load, true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", params.length);
		xmlhttp.setRequestHeader("Connection", "close");			
		xmlhttp.onreadystatechange = function() {				
			if (xmlhttp.readyState == 4) {
				//alert(xmlhttp.responseText);
				if (xmlhttp.status == 200) {							
					document.getElementById(div_load).innerHTML = xmlhttp.responseText;	
				}					
			}
		}
		xmlhttp.send(params);
	}
}
function LoadCommnet(div_load,id,Page){
	var link="http://phim.iyou.vn?page=loadcomment&id="+id+"&p="+Page;
	loadAjaxGet(link,div_load);
}
function LoadSendCommnet(div_load,IdP){
	var noidung=document.getElementById('noidung').value;
	noidung=noidung.replace('&','amp;');
	if(noidung.length<3){
		alert('Nội dung bình luận quá ngắn!');
	}else if(noidung.length>400){
		alert('Nội dung bình luận tối đa là 400 kí tự!');
	}else{
		var link_load="http://phim.iyou.vn?page=_comment";
		var params="idP="+IdP+"&nd="+noidung;
		loadAjaxPost(link_load,params,div_load);
	}	
}
function loadAjax(a,b){	
	var xmlhttp;	
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{				
			document.getElementById("AjaxSearch").innerHTML=xmlhttp.responseText;		
		}
	  }	
	if(a==1){	
		
		xmlhttp.open("GET","http://phim.iyou.vn?page=_AjaxSearch&type="+b,true);	
	}
	xmlhttp.send();
}
function LoadPage(link_load,div_load){
	document.getElementById("tabs_container").scrollIntoView(1);
	loadAjaxGet(link_load,div_load);
}
function CheckBoxRemem(a){
	var kt=document.getElementById(""+a).checked;
	if(kt==true){
		document.getElementById(""+a).checked=false;
	}else{
		document.getElementById(""+a).checked=true;	
	}
}
function OnLoad_Error(id){	
	var error_select=document.getElementById("error_select").value;
	var error_contend=document.getElementById("error_contend").value;
	var error_security=document.getElementById("error_security").value;
	
	if(error_contend.length>100){
		document.getElementById("tb_errorbox").innerHTML="Vui lòng nhập thông tin mô tả dưới 100 ký tự!";	
	}else if(error_security==''){
		document.getElementById("tb_errorbox").innerHTML="Vui lòng nhập mã xác nhận!";	
	}else{
		var link_load="/notice.html";
		var params="id="+id+"&sel="+error_select+"&ser="+error_security+"&mt="+error_contend;
		loadAjaxPost(link_load,params,"errorbox");
	}	
}
function OnLoad_Require(){	
	var Require_contend=document.getElementById("Require_contend").value;
	var Require_security=document.getElementById("Require_security").value;
	var Re_type=document.getElementById("re_type").value;
	
	if(Require_contend.length<5){
		document.getElementById("tb_Requirebox").innerHTML="Vui lòng nhập thông tin mô tả trên 5 ký tự!";	
	}else if(Require_contend.length>100){
		document.getElementById("tb_Requirebox").innerHTML="Vui lòng nhập thông tin mô tả dưới 100 ký tự!";	
	}else if(Require_security==''){
		document.getElementById("tb_Requirebox").innerHTML="Vui lòng nhập mã xác nhận!";	
	}else{
		var link_load="/require_film.html";
		var params="type="+Re_type+"&ser="+Require_security+"&content="+Require_contend;
		loadAjaxPost(link_load,params,"Requirebox");
	}	
}
function VotePhim(point,id){	
	var link_load="/vote.html";
	var params="id="+id+"&point="+point;
	loadAjaxPost(link_load,params,"unit_long2id");
}
function watch_later(id){	
	var link_load="/watch_later.html";
	var params="id="+id;
	loadAjaxPost(link_load,params,"watch_later_buttom");
}
function Paging(e,id,links, div_load, LastPage){		
	var keypressed = null;		
	if (window.event)
	{
		keypressed = window.event.keyCode; //IE
	}
	else
	{ 
		keypressed = e.which; //NON-IE, Standard
	}	

	if (keypressed < 48 || keypressed > 57)
	{ //CharCode c?a 0 là 48 (Theo b?ng mã ASCII)
	//CharCode c?a 9 là 57 (Theo b?ng mã ASCII)		
		if (keypressed == 8 || keypressed == 127)
		{//Phím Delete và Phím Back			
			return ;
		}else if(keypressed == 13){
			if(isNaN(id)==false){
				id=Math.floor(id);
				if(LastPage<id){
					id=LastPage;			
				}
				if(id<1){
					id=1;	
				}
			}else{
				id=1;	
			}
			var link_load=links+'/'+id+'.html';
			LoadPage(link_load,div_load);			
		} 						
			return false;
	}	
}
function Detect_speed(){
	var xmlhttp;	
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{				
			document.getElementById("bandwitch").value = xmlhttp.responseText;		
		}
	  }	
	xmlhttp.open("GET","http://s1.iyou.vn:81/index.php",true);
	xmlhttp.send();			
}
function trim(s)
{
	var l=0; var r=s.length -1;
	while(l < s.length && s[l] == ' ')
	{	l++; }
	while(r > l && s[r] == ' ')
	{	r-=1;	}
	return s.substring(l, r+1);
}
function registerid(){
	var u 		= document.register.user.value;
	var p 		= document.register.pass.value;
	var repass  = document.register.repass.value;
	var email 	= document.register.email.value;
	var phone 	= document.register.phone.value;
	var code 	= document.register.code.value;
	var e		= 1;
	if(u.length<6 || u.length>32){
		document.getElementById("register_e").innerHTML = "Tên đăng nhập phải từ 6 đến 32 ký tự.";
		e 		= 0;
	}else if(p.length<6 || p.length>32){
		document.getElementById("register_e").innerHTML = "Mật khẩu phải từ 6 đến 32 ký tự.";
		e 		= 0;
	}else if(p!=repass){
		document.getElementById("register_e").innerHTML = "Mật khẩu xác nhận không đúng.";
		e 		= 0;
	}else if(email==''){
		document.getElementById("register_e").innerHTML = "Vui lòng nhập Email.";
		e 		= 0;
	}else if(phone==''){
		document.getElementById("register_e").innerHTML = "Vui lòng nhập số điện thoại.";
		e 		= 0;
	}else if(code==''){
		document.getElementById("register_e").innerHTML = "Vui lòng nhập mã xác nhận.";
		e 		= 0;
	}
	if(e==1){
		var params="user="+u+"&pass="+p+"&email="+email+"&phone="+phone+"&code="+code;
		loadAjaxPost('/?page=_register',params,'registerid');	
	}
}

